package com.gxa.hualian.web.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.hualian.entity.Coupon;
import com.gxa.hualian.service.CouponService;
import com.gxa.hualian.service.impl.CouponServiceImpl;
import com.gxa.hualian.util.JsonResult;
import com.gxa.hualian.util.ResponseUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

//@WebServlet(name = "CouponServlet",urlPatterns = "/cpn")
public class CouponServlet extends HttpServlet {
    CouponService couponService=new CouponServiceImpl();
    // 声明JsonResult对象
    private JsonResult jsonResult = new JsonResult();
    private ResponseUtil responseUtil=null;
    private ObjectMapper objectMapper = new ObjectMapper();
    private void selectByPageHandler(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer page=Integer.parseInt(request.getParameter("page"));
        Integer limit=Integer.parseInt(request.getParameter("limit"));
        System.out.println(page+','+limit);
        List<Coupon> coupons = couponService.selectByPage(page, limit);
        List<Coupon> coupons1 = couponService.sellectAll();

        String count= String.valueOf(coupons1.size());

        jsonResult.setCode("0");
        jsonResult.setData(coupons);
        jsonResult.setCount(count);
        //2. 声明ResponseUtil对象,完成写操作
        responseUtil = new ResponseUtil(response,jsonResult);
        responseUtil.write();

    }
    private void updateHandler(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer cpnId = Integer.valueOf(request.getParameter("cpnId"));
        Boolean inUse = Boolean.valueOf(request.getParameter("inUse"));
        System.out.println(inUse);
        Coupon coupon = couponService.sellectById(cpnId);

        if (coupon.getInUsed()==true){
            coupon.setInUsed(false);
        }else{
            coupon.setInUsed(true);
        }
        couponService.update(coupon);

       jsonResult.setCode("1");

       //2. 声明ResponseUtil对象,完成写操作
        responseUtil = new ResponseUtil(response,jsonResult);
        responseUtil.write();

    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("CouponServlet 被访问了");
        // 创建ServletContext
        ServletContext servletContext = request.getServletContext();

        String encoding = servletContext.getInitParameter("encoding");

        // 接收前端的请求参数时,如果有中文, 也采用UTF-8编码格式来处理
        request.setCharacterEncoding(encoding);
        //获取请求路径

        String handlerType=request.getHeader("handlerType");
        System.out.println(handlerType);
        // 支持中文
        response.setCharacterEncoding(encoding);
        response.setContentType("application/json;charset="+encoding);
        try {
            if ("getList".equals(handlerType)){
                selectByPageHandler(request,response);
            }else if ("update".equals(handlerType)){
                updateHandler(request,response);
            }else if("add".equals(handlerType)){
                insertHandler(request,response);
            }
        }catch (Exception e){
            //当有异常发生,当前Servlet不做处理,统一转发给ExceptionServlet
            //利用request作用域对象,将e传递
            request.setAttribute("exception",e);
            //实现转发
            this.getServletContext().getRequestDispatcher("/error").forward(request,response);
        }

    }

    private void insertHandler(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //获取前端json字符串
        String jsonString=request.getParameter("formData");
        //利用工具将json字符串转换为对象
        Coupon coupon = objectMapper.readValue(jsonString, Coupon.class);
        //调用添加方法将转换的对象添加到数据库中
        coupon.setInUsed(false);
        couponService.insert(coupon);
        //返回前端值
        jsonResult.setCode("1");
        //2. 声明ResponseUtil对象,完成写操作
        responseUtil = new ResponseUtil(response,jsonResult);
        responseUtil.write();

    }
}
